package xcmg.device.service.vo.threeGuaranteesPur;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import yb.ecp.fast.infra.constants.ErrorCode;
import yb.ecp.fast.infra.infra.ActionResult;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

@Data
public class ThreeGuaranteesPurAddVO {
    private ThreeGuaranteesPurVO headerInfo;

    private List<ThreeGuaranteesPurDetailVO> detailList;
    //原始请求零件详情参数，塔机请求数据时对detailList参数做过一次整合（系统不支持同一个单据存在重复零件号的情况），把同零件不同条码的零件进行了数量合并，所以需要把原来的零件详情参数记录下来，用于后续业务操作的使用
    private List<ThreeGuaranteesPurDetailVO> originDetailList;

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
    }

    /**塔机请求参数校验*/
    public ActionResult tjParamCheck(){
        ActionResult actionResult = headInfoFiledCheck("deliveryTypeId", "deliveryTypeName", "dispatcherId", "dispatcherName",
                "sapId", "sapTypeName", "guaranteesTypeName", "sapName", "settleTypeId", "settleTypeName", "receiverAddress",
                "receiverName", "applicationId", "applicationName", "applicationPhone", "sapCode", "customerSapCode", "machineNumber",
                "prodType", "crmOrder", "serviceName", "serviceSite");
        //headInfo验证成功后验证detailList中的参数
        if(actionResult != null && actionResult.getCode() == ErrorCode.Success.getCode()){
            actionResult = detailListFiledCheck("materialsNo","purchaseQty","price","barcode");
        }
        return actionResult;
    }

    /**塔机请求参数校验*/
    public ActionResult cyParamCheck(){
        ActionResult actionResult = headInfoFiledCheck("deliveryTypeId", "deliveryTypeName", "dispatcherId", "dispatcherName",
                "sapId", "sapTypeName", "guaranteesTypeName", "sapName", "settleTypeId", "settleTypeName", "receiverAddress",
                "receiverName", "applicationId", "applicationName", "applicationPhone", "sapCode", "customerSapCode", "machineNumber",
                "prodType", "crmOrder", "serviceName", "serviceSite");
        //headInfo验证成功后验证detailList中的参数
        if(actionResult != null && actionResult.getCode() == ErrorCode.Success.getCode()){
            actionResult = detailListFiledCheck("materialsNo","purchaseQty","price","barcode");
        }
        return actionResult;
    }


    private ActionResult detailListFiledCheck(String ... fileds){
        for(String filed : fileds){
            if(!StringUtils.isEmpty(filed)){
                Method getMethod = getGetMethod(ThreeGuaranteesPurDetailVO.class, filed);
                if(getMethod == null){
                    continue;
                }
                try {
                    List<ThreeGuaranteesPurDetailVO> detailList = this.getDetailList();
                    for(ThreeGuaranteesPurDetailVO purDetailVO : detailList){
                        Object invoke = getMethod.invoke(purDetailVO, new Object[0]);
                        if(invoke == null || StringUtils.isEmpty(invoke.toString())){
                            return new ActionResult(yb.ecp.fast.infra.constants.ErrorCode.IllegalArument.getCode(),"参数"+filed+"不能为空",new TjThreeGuaranteesPurAddRespVO());
                        }
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        }
        return new ActionResult(yb.ecp.fast.infra.constants.ErrorCode.Success);
    }

    private ActionResult headInfoFiledCheck(String ... fileds){
        for(String filed : fileds){
            if(!StringUtils.isEmpty(filed)){
                Method getMethod = getGetMethod(ThreeGuaranteesPurVO.class, filed);
                if(getMethod == null){
                    continue;
                }
                try {
                    Object invoke = getMethod.invoke(this.getHeaderInfo(), new Object[0]);
                    if(invoke == null || StringUtils.isEmpty(invoke.toString())){
                        return new ActionResult(yb.ecp.fast.infra.constants.ErrorCode.IllegalArument.getCode(),"参数"+filed+"不能为空",new TjThreeGuaranteesPurAddRespVO());
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        }
        return new ActionResult(yb.ecp.fast.infra.constants.ErrorCode.Success);
    }

    @SuppressWarnings("unchecked")
    public static Method getGetMethod(Class objectClass, String fieldName) {
        StringBuffer sb = new StringBuffer();
        sb.append("get");
        sb.append(fieldName.substring(0, 1).toUpperCase());
        sb.append(fieldName.substring(1));
        try {
            return objectClass.getMethod(sb.toString());
        } catch (Exception e) {
            return null;
        }
    }
}
